/*****************************************************************************************
**  Author:jxx 2022
**  QQ:283591387
**完整文档见：http://v2.volcore.xyz/document/api 【代码生成页面ViewGrid】
**常用示例见：http://v2.volcore.xyz/document/vueDev
**后台操作见：http://v2.volcore.xyz/document/netCoreDev
*****************************************************************************************/
//此js文件是用来自定义扩展业务代码，可以扩展一些自定义页面或者重新配置生成的代码
import moment from "moment/moment";
import extendHeader from "./fc_yszk_import.vue"
import http from "@/api/http.js";
import { ITEM_RENDER_EVT } from "element-plus/es/components/virtual-list/src/defaults";
let extension = {
  components: {
    //查询界面扩展组件
    //gridHeader: '',
    //gridHeader: () => import("./fc_yszk_import.vue"), 
    gridHeader: extendHeader, 
    gridBody: '',
    gridFooter: '',
    //新建、编辑弹出框扩展组件
    modelHeader: '',
    modelBody: '',
    modelFooter: ''
  },
  tableAction: '', //指定某张表的权限(这里填写表名,默认不用填写)
  buttons: { view: [], box: [], detail: [] }, //扩展的按钮
  methods: {
     //下面这些方法可以保留也可以删除
    onInit() {  //框架初始化配置前，
        //示例：在按钮的最前面添加一个按钮
        //   this.buttons.unshift({  //也可以用push或者splice方法来修改buttons数组
        //     name: '按钮', //按钮名称
        //     icon: 'el-icon-document', //按钮图标vue2版本见iview文档icon，vue3版本见element ui文档icon(注意不是element puls文档)
        //     type: 'primary', //按钮样式vue2版本见iview文档button，vue3版本见element ui文档button
        //     onClick: function () {
        //       this.$Message.success('点击了按钮');
        //     }
        //   });

        //示例：设置修改新建、编辑弹出框字段标签的长度
        // this.boxOptions.labelWidth = 150;
        this.setFiexdSearchForm(true);
        this.columns.forEach(column => {
          //格式化日期
          if (column.field == 'duedate') {
            column.formatter = (row) => {
              return (row.duedate || '').split(' ')[0].replace(/-/g, '-').substr(0,7)
            }
          }
        })
    },
    import() {//覆盖导入事件
      //this.$refs.gridHeader.model = true;
      this.$refs.gridHeader.openModel();
    },
    onInited() {
      //框架初始化配置后
      //如果要配置明细表,在此方法操作
      //this.detailOptions.columns.forEach(column=>{ });
      this.searchFormFields.duedate =moment().month(moment().month() - 1).format("YYYY-MM");
      this.singleSearch = null;
      this.columns.splice(0);
      this.columns.push(...[{field:'cw_fc_yszk_id',title:'飞彩应收账款表',type:'guid',width:110,hidden:true,readonly:true,require:true,align:'left'},
      {field:'duedate',title:'应收日期',type:'datetime',width:110,align:'left',sort:true},
      {field:'trackname',title:'赛道',type:'string',width:220,align:'left'},
      {field:'category',title:'分类',type:'string',bind:{ key:'cw_yszg_category',data:[]},width:220,align:'left'},
      {field:'beginyearnum',title:'年初数',type:'decimal',width:110,align:'left'},
      {field:'endyearreceivable',title:moment().year(moment().year() - 1).format("YYYY")+'年末应收',type:'decimal',width:110,align:'left'},
      {field:'customer',title:'客户',type:'string',width:220,align:'left'},
      {field:'monthreceivable',title:'月应收',type:'decimal',width:110,align:'left'},
      {field:'isoverdue',title:'是否逾期',type:'string',bind:{ key:'cw_yszk_isoverdue',data:[]},width:220,align:'left'},
      {field:'iscushion',title:'是否代垫',type:'string',bind:{ key:'cw_yszk_iscushion',data:[]},width:220,align:'left'},
      {field:'cushiontype',title:'代垫类型',type:'string',width:220,align:'left'},
      {field:'collectionpolicy',title:'收款政策',type:'string',width:220,align:'left'},
      {field:'accountperiod',title:'账期',type:'string',width:220,align:'left'},
      {field:'problem',title:'存在的问题',type:'string',width:220,align:'left'},
      {field:'measure',title:'措施',type:'string',width:220,align:'left'},
      {field:'receivabledate',title:'应收到期日',type:'datetime',width:150,align:'left',sort:true},
      {field:'invoicingdate',title:'开票日期',type:'datetime',width:150,align:'left',sort:true},
      {field:'personliable',title:'责任人',type:'string',width:220,align:'left'},
      {field:'Name1',title:'账龄',type:'string',width:220,align:'center',
      children: [
        {field:'aging1',title:'1-3个月',type:'decimal',width:110,align:'left'},
        {field:'aging2',title:'3-6个月',type:'decimal',width:110,align:'left'},
        {field:'aging3',title:'6-12个月',type:'decimal',width:110,align:'left'},
        {field:'aging4',title:'1-2年',type:'decimal',width:110,align:'left'},
        {field:'aging5',title:'2-3年',type:'decimal',width:110,align:'left'},
        {field:'aging6',title:'3年以上',type:'decimal',width:110,align:'left'},
      ]
      },
      {field:'Name2',title:'N+3回款预测',type:'string',width:220,align:'center',
      children: [
        {field:'returnedmoney1',title:moment().month(moment(this.searchFormFields.duedate).month()+1).format("M")+'月',type:'decimal',width:110,align:'left'},
        {field:'returnedmoney2',title:moment().month(moment(this.searchFormFields.duedate).month()+2).format("M")+'月',type:'decimal',width:110,align:'left'},
        {field:'returnedmoney3',title:moment().month(moment(this.searchFormFields.duedate).month()+3).format("M")+'月',type:'decimal',width:110,align:'left'},
      ]
      },
      {field:'customerproperty',title:'客户属性',type:'string',width:220,align:'left'},
      {field:'creditrating',title:'信用等级',type:'string',width:220,align:'left'},
      {field:'ordercontract',title:'订单合同',type:'string',width:220,align:'left'},
      {field:'beginmonthbalance',title:'月初余额',type:'string',width:220,align:'left'},
      {field:'agingstr',title:'账龄',type:'string',width:220,align:'left'},
      {field:'warningamount',title:'预警金额',type:'string',width:220,align:'left'},
      {field:'remark',title:'备注',type:'string',width:220,align:'left'}]
      )

      this.columns.forEach(column => {
        //格式化日期
        if (column.field == 'duedate') {
          column.formatter = (row) => {
            return (row.duedate || '').split(' ')[0].replace(/-/g, '-').substr(0,7)
          }
        }
      })
    },
    searchBefore(param) {
      //界面查询前,可以给param.wheres添加查询参数
      //返回false，则不会执行查询
      if(this.searchFormFields.duedate == null){
        this.$message.error("请选择应收日期");
        return false;
      }
      return true;
    },
    searchAfter(result) {
      //查询后，result返回的查询数据,可以在显示到表格前处理表格的值
      this.columns.forEach(x => {
        if (x.field == "endyearreceivable") {
          x.title = moment().year(moment(this.searchFormFields.duedate).year()-1).format("YYYY") + '年末应收';
        }
        if (x.field == "monthreceivable") {
          x.title = moment().year(moment(this.searchFormFields.duedate).year()).format("YYYY") + '年' + moment().month(moment(this.searchFormFields.duedate).month()).format("M") + '月应收';
        }
        if (x.field == "Name2") {
         
          x.children.forEach(y => {
            if (y.field == "returnedmoney1") {
              y.title = moment().month(moment(this.searchFormFields.duedate).month()+1).format("M")+'月'
            }
            if (y.field == "returnedmoney2") {
              y.title = moment().month(moment(this.searchFormFields.duedate).month()+2).format("M")+'月'
            }
            if (y.field == "returnedmoney3") {
              y.title = moment().month(moment(this.searchFormFields.duedate).month()+3).format("M")+'月'
            }
          }) 
        }
      }) 

      return true;
    },
    addBefore(formData) {
      //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
      return true;
    },
    updateBefore(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      return true;
    },
    rowClick({ row, column, event }) {
      //查询界面点击行事件
      // this.$refs.table.$refs.table.toggleRowSelection(row); //单击行时选中当前行;
    },
    async modelOpenBeforeAsync(row) {
      let url = '/api/fc_yszk/checkIsApproved?yearmonth='+this.searchFormFields.duedate;
      let temp = true;
      var _result = await this.http.post(url, {}, true).then(result => {
      _result = result;
        if(_result.code == -1){
          this.$message.error(this.searchFormFields.duedate+"数据已审核，不能再进行编辑操作！");
          temp =false;
        }else{
          temp = true;
        }
      })
      return temp;
     },
    modelOpenBefore (row) {
     
    },
    modelOpenAfter(row) {
      //点击编辑、新建按钮弹出框后，可以在此处写逻辑，如，从后台获取数据
      //(1)判断是编辑还是新建操作： this.currentAction=='Add';
      //(2)给弹出框设置默认值
      //(3)this.editFormFields.字段='xxx';
      //如果需要给下拉框设置默认值，请遍历this.editFormOptions找到字段配置对应data属性的key值
      //看不懂就把输出看：console.log(this.editFormOptions)
      this.editFormFields.trackname = "飞彩";
      this.editFormOptions.forEach(item => {
        item.forEach(x => {
                //如果是编辑帐号设置为只读
                if (x.field == "trackname") {
                    //disabled是editFormOptions的动态属性，这里只能通过this.$set修改值
                    //vue3版本改为设置：x.disabled=isEDIT
                    x.disabled=true;
                }
            })
    })
    }
  }
};
export default extension;
